﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;

namespace FileCompareByHash
{
    public partial class DetailedComparison : Form
    {
        public FileInfo _file1;
        public FileInfo _file2;
        public bool _bSame;
        public DetailedComparison(FileInfo file1, FileInfo file2, bool bSame)
            : this()
        {
            _file1 = file1;
            _file2 = file2;
            _bSame = bSame;
            _label1.Text = "File1:" + _file1.FullName;
            _label2.Text = "File2:" + _file2.FullName;
            _bSame = bSame;
        }
        public DetailedComparison()
        {
            InitializeComponent();
        }

        private void _btnOK_Click(object sender, EventArgs e)
        {
            Close();
        }
        List<CompInfo> _properties = new List<CompInfo>(4);
        const string _original = "Click this cell to show the hash";
        private void DetailedComparison_Load(object sender, EventArgs e)
        {

            LoadGrid();

        }

        private void LoadGrid()
        {
            _properties.RemoveAll(_=> true);
            _properties.Add(new CompInfo { Property = "SizeOnDisk", File1 = _file1.Length.ToString("0,0"), File2 = _file2.Length.ToString("0,0") });
            _properties.Add(new CompInfo { Property = "Modified", File1 = _file1.LastWriteTime.ToString(), File2 = _file2.LastWriteTime.ToString() });
            _properties.Add(new CompInfo { Property = "MD5 Hash", File1 = _original, File2 = _original });
            _properties.Add(new CompInfo { Property = "SHA1 Hash", File1 = _original, File2 = _original });
            _properties.Add(new CompInfo { Property = "SHA256 Hash", File1 = _original, File2 = _original });
            UpdateGrid();
            _dataGridView.Columns["Property"].Width = 55;
            _dataGridView.Columns["Hidden"].Visible = false;
            // _dataGridView.Refresh();
        }

        private void UpdateGrid()
        {
            _dataGridView.DataSource = (from p in _properties select new { Property = p.Property, File1 = p.File1, File2 = p.File2, Hidden = p }).ToArray();
        }

        private void _dataGridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.RowIndex < 0)
                    return;
                this.UseWaitCursor = true;
                Application.DoEvents();
                string prop = _dataGridView.Rows[e.RowIndex].Cells["Property"].Value as string;
                if (prop.EndsWith("hash", StringComparison.InvariantCultureIgnoreCase))
                {
                    CompInfo ci = _dataGridView.Rows[e.RowIndex].Cells["Hidden"].Value as CompInfo;
                    if (_original != _dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string)
                        return;
                    if (prop.StartsWith("md5", StringComparison.InvariantCultureIgnoreCase))
                    {

                        if (_dataGridView.Columns[e.ColumnIndex].Name == "File1")
                        {
                            ci.File1 = FileCompareByHashExtension.MultiFileCompareByHash.CalcHash<MD5CryptoServiceProvider>(_file1.FullName);
                            if (_bSame)
                                ci.File2 = ci.File1;
                        }
                        else
                        {
                            ci.File2 = FileCompareByHashExtension.MultiFileCompareByHash.CalcHash<MD5CryptoServiceProvider>(_file2.FullName);
                            if (_bSame)
                                ci.File1 = ci.File2;
                        }
                    }
                    else if (prop.StartsWith("sha1", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (_dataGridView.Columns[e.ColumnIndex].Name == "File1")
                        {
                            ci.File1 = FileCompareByHashExtension.MultiFileCompareByHash.CalcHash<SHA1CryptoServiceProvider>(_file1.FullName);
                            if (_bSame)
                                ci.File2 = ci.File1;
                        }
                        else
                        {
                            ci.File2 = FileCompareByHashExtension.MultiFileCompareByHash.CalcHash<SHA1CryptoServiceProvider>(_file2.FullName);
                            if (_bSame)
                                ci.File1 = ci.File2;
                        }

                    }
                    else if (prop.StartsWith("sha256", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (_dataGridView.Columns[e.ColumnIndex].Name == "File1")
                        {
                            ci.File1 = FileCompareByHashExtension.MultiFileCompareByHash.CalcHash<SHA256CryptoServiceProvider>(_file1.FullName);
                            if (_bSame)
                                ci.File2 = ci.File1;
                        }
                        else
                        {
                            ci.File2 = FileCompareByHashExtension.MultiFileCompareByHash.CalcHash<SHA256CryptoServiceProvider>(_file2.FullName);
                            if (_bSame)
                                ci.File1 = ci.File2;
                        }

                    }

                    UpdateGrid();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            finally
            {
                this.UseWaitCursor = false;
            }
        }

        private void _btnRefresh_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }
    }

    class CompInfo
    {
        internal string Property { set; get; }
        internal string File1 { set; get; }
        internal string File2 { set; get; }
    }
}
